/**
 * 
 */
package com.yidu.drug.action;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Message;
import com.yidu.drug.common.Page;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.FdPoBreakdownModel;
import com.yidu.drug.model.FdPurchaseModel;
import com.yidu.drug.model.SalesReturnModel;
import com.yidu.drug.service.FdPoBreakdownModelService;
import com.yidu.drug.service.FdPurchaseService;

/**
 * 类的描述    分店订单详细表
 * @author xiaodozi
 * @date 2018年8月14日 上午8:51:33 
 * @version 1.0
 */
@Controller
@RequestMapping("/FdPoBreakdown")
public class FdPoBreakdownAction {
	
	@Resource
	private FdPoBreakdownModelService fdPoBreakdownModelService;
	
	@Resource
	private FdPurchaseService fdPurchaseService;
	
	
	/**
	 * 查询所有和条件查询分页--退货单表
	 * @param response 响应
	 * @param reCause 传入参数退货原因
	 * @param reOper 传入参数操作人
	 * @param page 传入参数页面
	 * @param limit 传入分页
	 * @return maps
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String, Object> findAll(HttpServletResponse response,String reCause,String reOper,int page,int limit) {
		System.out.println("进入findAll方法");
		response.setCharacterEncoding("UTF-8");
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		Map<String,String> map=new HashMap<>();
		//根据条件查询的搜索框
		map.put("reCause", reCause);
		map.put("reOper", reOper);
		map.put("start", String.valueOf(pages.getStart()));
		map.put("rows", String.valueOf(pages.getRows()));
		List<FdPoBreakdownModel> list = fdPoBreakdownModelService.findAllPage(map);
		
		Map<String, Object> maps = new HashMap<>();
		FdPoBreakdownModel fdPoBreakdown = new FdPoBreakdownModel();
		//需要符合layUI格式，code msg count data
		maps.put("code", 0);
		maps.put("msg", "1");
		maps.put("count", fdPoBreakdownModelService.findTotalRows(fdPoBreakdown));
		maps.put("data", list);
			
		return maps;
	}
	
	
	/**
	 * 分页查询
	 * @param session
	 * @param fdPoBreakdownModel
	 * @return
	 */
	@RequestMapping("/findRow")
	@ResponseBody
	public Page findRow(HttpSession session,FdPoBreakdownModel fdPoBreakdownModel){
		int total=fdPoBreakdownModelService.findRows(fdPoBreakdownModel);
		System.out.println("总行数"+total+"---------------------");
		Page page = new Page();
		int pages = 0;
		if(total % 10 == 0){
			pages = total / 10;
		}else {
			pages = total / 10 + 1;
		}
		page.setPage(pages);
		page.setTotalRows(total);
		page.setRows(10);
		return page;
		
	}
	
	/**
	 * 根据订单id查询订单详细表中的数据
	 * @param fdPoId
	 * @return
	 */
	@RequestMapping("/findByfdPoId")
	@ResponseBody
	public Map<String, Object> findByfdPoId(HttpSession session,String fdPoId,int page,int limit,String drugName){
		page=(page-1)*limit;
		System.out.println(fdPoId+"====================================================================");
		FdPoBreakdownModel fdPoBreakdownModel=new FdPoBreakdownModel();
		fdPoBreakdownModel.setFdPoId(fdPoId);
		fdPoBreakdownModel.setDrugName(drugName);
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		List<FdPoBreakdownModel> list=fdPoBreakdownModelService.findByfdPoId(fdPoBreakdownModel,page,limit);
		Map<String, Object> map=new HashMap<String , Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",fdPoBreakdownModelService.findRows(fdPoBreakdownModel));
		map.put("data", list);
		return map;
		
	}
	/**
	 * 根据id查询数据
	 * @param session
	 * @param fdPoId
	 * @param page
	 * @param limit
	 * @param drugName
	 * @return
	 */
	@RequestMapping("/findById")
	@ResponseBody
	public Map<String, Object> findById(HttpSession session,String fdPoId,int page,int limit,String drugName){
		page=(page-1)*limit;
		System.out.println(fdPoId+"=====================================================");
		FdPoBreakdownModel fdPoBreakdownModel=new FdPoBreakdownModel();
		fdPoBreakdownModel.setFdPoId(fdPoId);
		fdPoBreakdownModel.setDrugName(drugName);
		Page pages = new Page();
		pages.setPage(page);
		pages.setRows(limit);
		List<FdPoBreakdownModel> list=fdPoBreakdownModelService.findByfdPoId(fdPoBreakdownModel,page,limit);
		Map<String, Object> map=new HashMap<String , Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",fdPoBreakdownModelService.findRows(fdPoBreakdownModel));
		map.put("data", list);
		return map;
		
	}
	
	/**
	 * 增加
	 * @param session
	 * @param fdPoBreakdownModel
	 * @param arr
	 * @param fdPoId
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public String save(HttpSession session,FdPoBreakdownModel fdPoBreakdownModel,String arr,String fdPoId) {
		System.out.println(arr+"=========================================");
		
		Gson gosn=new Gson();
		//创建一个提示消息的对象
		Message msg = new Message();
		//使用Type类，取得相应类型对象的class属性
		java.lang.reflect.Type listType=new TypeToken<ArrayList<DrugModel>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
		//使用集合接受将json转换为java对象集合后的数据
		ArrayList<DrugModel> list = gosn.fromJson(arr, listType);
		for (DrugModel drugModel : list) {
			System.out.println(drugModel.getDrugId()+"==========================================");
			System.out.println(drugModel.getDrugPrice()+"------------------------------------");
			
			String fdPodId=UUID.randomUUID().toString().replaceAll("-", "");
			fdPoBreakdownModel.setFdPodId(fdPodId);
			fdPoBreakdownModel.setFdPoId(fdPoId);
			fdPoBreakdownModel.setDrugId(drugModel.getDrugId());
			fdPoBreakdownModel.setFdPrice(drugModel.getDrugPrice());
			fdPoBreakdownModel.setFdUnit(drugModel.getPov());
			fdPoBreakdownModelService.save(fdPoBreakdownModel);
			
		}
		return arr;
	}
	/**
	 * 删除
	 * @param fdPodId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String fdPodId){
		System.out.println(fdPodId+"============================================================");
		int rows=fdPoBreakdownModelService.delete(fdPodId);
		if(rows > 0) {
			return "ok";
		}else {
			return "no";
		}
	}
	/**
	 * 根据id修改数量和物品备注
	 * @param fdItemNumber
	 * @param fdItemRemark
	 * @param fdSubtotal
	 * @param fdPodId
	 * @param fdPrice
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(Integer fdItemNumber,String fdItemRemark,Integer fdSubtotal,String fdPodId,Integer fdPrice){
		System.out.println(fdItemNumber+"====================");
		System.out.println(fdItemRemark+"====================");
		fdSubtotal=fdItemNumber*fdPrice;
		System.out.println(fdSubtotal+"======================");
		System.out.println(fdPodId+"============================================");
		
		return fdPoBreakdownModelService.update(fdItemNumber, fdItemRemark, fdSubtotal, fdPodId);
	}
	
	@RequestMapping("/findAllsl")
	@ResponseBody
	public String findAllsl(String fdPoId){
		Integer sl=fdPoBreakdownModelService.findAllsl(fdPoId);
		Integer jg=fdPoBreakdownModelService.findAlljg(fdPoId);
		
		System.out.println(sl+"数量========================================");
		System.out.println(jg+"价格=========================");
		fdPurchaseService.updates(sl.longValue(), new BigDecimal(jg), fdPoId);
		return "ok";
	}
}
